import time

import pytest
import requests

from pingtai.configs import PT_BASIC_URI
from pingtai.configs.apps import jf_app
from pingtai.utills.pt_utils import get_pt_sign
from utills.send import update_tmp_db


@pytest.fixture(scope="session")
def jf_access_token():
    """获取应用令牌access_token
    业务系统调用平台接口，均需业务先获得合法应用令牌
    """
    session = requests.Session()
    params = {
        "app_id": jf_app.get("app_id"),
        "app_secret": jf_app.get("app_secret"),
        "platform": "1",
        "timestamp": str(int(time.time()))
    }
    params["sign"] = get_pt_sign(jf_app, params) # 所有调平台接口，均需按照平台签名规则进行签名，防止非法请求。
    resp = session.post(PT_BASIC_URI+"/v1/oauth2/access_token", params)
    if resp.status_code != 200 or resp.json().get("code") != 1001:
        print("获取应用令牌失败:", resp.text)
    update_tmp_db("refactor_demo_access_token", resp.json())
    access_token = resp.json()["data"]["access_token"]
    return access_token
